package com.camel.network.controller;

import cn.hutool.json.JSONObject;
import com.camel.core.config.CtiConfig;
import com.camel.core.entity.Result;
import com.camel.core.utils.HttpUtils;
import com.camel.core.utils.ResultUtil;
import com.camel.network.config.CtiConstants;
import com.camel.network.config.UrlConstants;
import com.camel.network.model.ZsPstnnumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RequestMapping("/manager")
@RestController
public class CtiManageController extends NetBaseController{

    @Autowired
    private CtiConfig ctiConfig;

    /**
     * 获取队列坐席号码信息
     *
     * @param params
     * @return
     */
    @PostMapping("/getQueueSeats")
    public Result getQueueSeat(@RequestBody JSONObject params) {
        JSONObject json = (JSONObject) HttpUtils.post(params, ctiConfig.getBaseUrl(), UrlConstants.CTI_GET_QUEUE_AGENT_INFO_URL);
        return ResultUtil.success(json.getJSONArray(CtiConstants.INFO));
    }

    /**
     * 获取外呼接入号
     *
     * @param params
     * @return
     */
    @PostMapping("/getpstnnumber")
    public Result getpstnnumber(@RequestBody JSONObject params) {
        params = new JSONObject();
        JSONObject json = (JSONObject) HttpUtils.post(params, ctiConfig.getBaseUrl(), UrlConstants.CTI_GETPSTNNUMBER_URL);
        if (json.getStr(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_UNAUTHENTICATION)) {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, json.getStr(CtiConstants.CTI_MESSAGE));
        } else {
            return ResultUtil.success(getList(json));
        }
    }

    /**
     * 新增外呼热线号码
     * @param entity
     * @return
     */
    @PostMapping("/createPstNumber")
    public Result createPstNumber(@RequestBody ZsPstnnumber entity) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("pstnNumber", entity.getPstnnumber());
        jsonObject.set("sipName", entity.getGateway());
        JSONObject json = (JSONObject) HttpUtils.post(jsonObject, ctiConfig.getBaseUrl(), UrlConstants.CTI_ADD_PSTNNUMBER_URL);
        if (!json.getStr(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_SUCCESS)) {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, json.getStr(CtiConstants.CTI_MESSAGE));
        } else {
            return ResultUtil.success();
        }
    }


    /**
     * 删除外呼热线号码
     * @param entity
     * @return
     */
    @PostMapping("/deletePstNumber")
    public Result deletePstNumber(@RequestBody ZsPstnnumber entity) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("pstnNumber", entity.getPstnnumber());
        JSONObject json = (JSONObject) HttpUtils.post(jsonObject, ctiConfig.getBaseUrl(), UrlConstants.CTI_DEL_PSTNNUMBER_URL);
        if (!json.getStr(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_SUCCESS)) {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, json.getStr(CtiConstants.CTI_MESSAGE));
        } else {
            return ResultUtil.success();
        }
    }


    /**
     * 分配热线号码
     * @param entity
     * @return
     */
    @PostMapping("/hotPstNumber")
    public Result hotPstNumber(@RequestBody ZsPstnnumber entity) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("pstnNumber", entity.getPstnnumber());
        jsonObject.set("lines", entity.getLine());
        jsonObject.set("companyNo", entity.getCompanyNo());
        jsonObject.set("areaCode", entity.getAreaCode());
        JSONObject json = (JSONObject) HttpUtils.post(jsonObject, ctiConfig.getBaseUrl(), UrlConstants.CTI_HOST_PSTNNUMBER_URL);
        if (!json.getStr(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_SUCCESS)) {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, json.getStr(CtiConstants.CTI_MESSAGE));
        } else {
            return ResultUtil.success();
        }
    }

    /**
     * 座席状态获取
     * {agentNo: "xxxx"}
     * 座席工号（不支持指定获取多个座席的状态，仅支持获取一个指定座席的状态或不填写获取所有座席的状态）
     * @param params
     * @return
     */
    @PostMapping("/getAgentStatus")
    public Result getAgentStatus(@RequestBody JSONObject params) {
        JSONObject json = (JSONObject) HttpUtils.get(ctiConfig.getBaseUrl(), UrlConstants.CTI_AGENT_STATUS_URL);
        if (json.getStr(CtiConstants.STATUSCODE).equals(CtiConstants.STATUS_UNAUTHENTICATION)) {
            return ResultUtil.error(ResultUtil.SYSTEM_ERR_CODE, json.getStr(CtiConstants.CTI_MESSAGE));
        } else {
            return ResultUtil.success(json.getJSONArray(CtiConstants.INFO));
        }
    }
}
